package com.micro.modual.extension;

import com.micro.common.MicroConstant;
import com.micro.common.MicroResponseCode;
import com.micro.common.biz.GeneratorCodeUtil;
import com.micro.common.util.ObjectUtil;
import com.micro.common.util.SnowFlakeUtil;
import com.micro.core.exception.BizException;
import com.micro.modual.entity.SystemUser;
import com.micro.modual.entity.SystemUserRole;
import com.micro.modual.model.bo.systemuser.BindRoleBO;
import com.micro.modual.model.bo.systemuser.SystemUserBO;
import com.micro.modual.model.dto.systemuserrole.UserRoleDTO;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <h1>系统用户表 延展类</h1>
 *
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:23:26
 */
public class SystemUserExtension {


    /**
     * <h2>转换系统用户</h2>
     * @param systemUserBO   用户基础对象
     * @param userCode          用户编号
     * @param passWordSalt      用户密码盐
     * @return
     */
    public static SystemUser converAddSystemUser(SystemUserBO systemUserBO, String userCode, String passWordSalt) {
        SystemUser systemUser = new SystemUser();
        systemUser.setId(SnowFlakeUtil.getId());
        systemUser.setUserCode(userCode);
        systemUser.setCreateTime(new Date());
        systemUser.setPasswordSalt(passWordSalt);
        systemUser.setUserAvatar(ObjectUtil.isEmpty(systemUserBO.getUserAvatar()) ? MicroConstant.SystemDefaultValue.DEFAULT_AVATAR:systemUserBO.getUserAvatar());
        systemUser.setUserEmail(systemUserBO.getUserEmail());
        systemUser.setUserPhone(systemUserBO.getUserPhone());
        systemUser.setUserStatus(systemUserBO.getUserStatus());
        systemUser.setUserNickName(systemUserBO.getUserNickName());
        systemUser.setUserSex(systemUserBO.getUserSex());
        systemUser.setPasswordStatus((byte)1);
        return systemUser;
    }

    /**
     * <h2>转换用户角色关联</h2>
     * @param systemUser       用户信息
     * @param roleList         拥有角色列表
     * @return
     */
    public static UserRoleDTO converUserRoleDTO(SystemUser systemUser, List<String> roleList) {
        UserRoleDTO userRoleDTO = new UserRoleDTO();
        userRoleDTO.setUserCode(systemUser.getUserCode());
        userRoleDTO.setUserName(systemUser.getUserNickName());
        userRoleDTO.setRoleCode(roleList);
        return userRoleDTO;
    }

    /**
     * <h2>绑定用户角色</h2>
     * @param bindRoleBO
     * @return
     */
    public static List<SystemUserRole> converBindUserRole(BindRoleBO bindRoleBO) {
        List<SystemUserRole> systemUserRoleList = new ArrayList<>();
        if(CollectionUtils.isEmpty(bindRoleBO.getRoleCode())){
            throw new BizException(MicroResponseCode.SELECT_AT_LEAST_ONE_ROLE.getResponseCode(),MicroResponseCode.SELECT_AT_LEAST_ONE_ROLE.getResponseMessage());
        }else {
            for(String roleCode:bindRoleBO.getRoleCode()){
                SystemUserRole systemUserRole = new SystemUserRole();
                systemUserRole.setRoleCode(roleCode);
                systemUserRole.setUserCode(bindRoleBO.getUserCode());
                systemUserRoleList.add(systemUserRole);
            }
        }
        return systemUserRoleList;
    }
}
